package com.service.menu;

import com.entity.Menu;
import com.facade.menu.IMenuFacadeR;
import com.mapper.CustomMapper;
import com.mapper.MenuMapper;
import com.service.CustomServiceR;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceR extends CustomServiceR<Menu> implements IMenuFacadeR {

    @Resource
    private MenuMapper mapper;

    @Override
    protected CustomMapper<Menu> initMapper() {
        return mapper;
    }

    @Override
    public List<Menu> findByUserLink(Serializable userId, String... sortOrder) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        StringBuilder sb0 = new StringBuilder();
        if (null != sortOrder) {
            for (int i = 0; i < sortOrder.length; i++) {
                sb0.append(sortOrder[i]);
                if (i != sortOrder.length - 1) {
                    sb0.append(", ");
                }
            }
        }
        params.put("sortOrder", sb0.toString());
        return mapper.findByUserLink(params);
    }

    @Override
    public List<Menu> findRankByUserLink(Serializable userId, String... sortOrder) {
        /*用户所有菜单List*/
        return findMenuLv(findByUserLink(userId, sortOrder));
    }

    @Override
    public List<Menu> findByRoleLink(Serializable roleId, String... sortOrder) {
        Map<String, Object> params = new HashMap<>();
        params.put("roleId", roleId);
        StringBuilder sb0 = new StringBuilder();
        if (null != sortOrder) {
            for (int i = 0; i < sortOrder.length; i++) {
                sb0.append(sortOrder[i]);
                if (i != sortOrder.length - 1) {
                    sb0.append(", ");
                }
            }
        }
        params.put("sortOrder", sb0.toString());
        return mapper.findByRoleLink(params);
    }

    @Override
    public List<Menu> findRankAll(String... sortOrder) {
        return findMenuLv(findAll(sortOrder));
    }

    /*菜单等级处理*/
    @Override
    public List<Menu> findMenuLv(List<Menu> menuList) {
        /*顶层菜单List*/
        List<Menu> topMenuList = new ArrayList<>();
        /*需要递归的子菜单List*/
        List<Menu> recursiveMenuList = new ArrayList<>();
        for (Menu menu : menuList) {
            if (null != menu.getPid()) {
                recursiveMenuList.add(menu);
            } else {
                topMenuList.add(menu);
            }
        }
        for (Menu topMenu : topMenuList) {
            menuRank(topMenu, recursiveMenuList);
        }
        return topMenuList;
    }

    private void menuRank(Menu menu, List<Menu> menuList) {
        for (Menu menu$ : menuList) {
            if (menu.getId().equals(menu$.getPid())) {
                menu.getChildren().add(menu$);
                menuRank(menu$, menuList);
            }
        }
    }
}
